<?php
//verification des package
include($project_path.'/install_report.php5');
$error = false;
if(!isset($_SESSION['install']['packages_versions']))
{
	echo 'updateDisplayError("«!ADMIN_INSTALL_ERROR_NOVERSIONS»");';
	$error = true;
}
else
{
	if(count($_SESSION['install']['packages_versions'])==0)
	{
		$_SESSION['install']['screen'] = 9;
		echo 'updateStopProgress();
updatePerform("'.$_SESSION['install']['id'].'", "«!ADMIN_INSTALL_END_HEAD»", "«!ADMIN_INSTALL_END_STATUS»");';
		$error = true;
	}
	else
	{
		$_SESSION['install']['package'] = array_shift($_SESSION['install']['packages_versions']);
		$temp_path = $project_path.'/temp/';
		$file = $_SESSION['install']['package']['name'].'.'.str_replace('.', '_', $_SESSION['install']['package']['version']).'.update.tgz';
		if(!file_exists($temp_path.$file) || filesize($temp_path.$file)!=$_SESSION['install']['package']['size'] || md5_file($temp_path.$file)!=$_SESSION['install']['package']['md5'])
		{
			echo 'updateDisplayError("«!ADMIN_INSTALL_ERROR_DOWNLOADING»");';
			$error = true;
		}
	}
}
if(!$error)
{
	include_once($project_path.'/class/ext/archive/gzip.class.php5');
	$tgz = new gzip_file($temp_path.$file);
	$tgz->set_options(array('basedir' => $temp_path, 'overwrite' => 1));
	list($errors, $n_succes) = $tgz->extract_files(array(array('', 'index.xml')));
	if(!is_file($temp_path.'index.xml'))
	{
		echo 'updateDisplayError("«!ADMIN_INSTALL_ERROR_PACK»");';
		$error = true;
	}
}
if(!$error)
{
	$setupXML = file_get_contents($temp_path.'index.xml');
	@unlink($temp_path.'index.xml');
	if($setupXML===false)
	{
		echo 'updateDisplayError("«!ADMIN_INSTALL_ERROR_PACK»");';
		$error = true;
	}
}
if(!$error)
{
	$_SESSION['install']['package']['index'] = $setupXML;
	$setup = @new SimpleXMLElement($_SESSION['install']['package']['index']);
	if(fwTools::compareVersions($setup['version'], '1.0')>0)
	{
		echo 'updateDisplayError("«!ADMIN_INSTALL_ERROR_PACKVERSION»");';
		$error = true;
	}
}
if(!$error)
{
	if(''.$setup->application[0]->name[0] != $_SESSION['install']['package']['name'])
	{
		echo 'updateDisplayError("«!ADMIN_INSTALL_ERROR_BADAPPLICATION»");';
		$error = true;
	}
}
if(!$error)
{
	$screens = array();
	
	if(count($setup->screens[0]->requirement)>0)
	{
		$screens[] = 3;
	}
	if(count($setup->screens[0]->extractfile)>0)
	{
		$screens[] = 4;
	}
	if(count($setup->screens[0]->createtable)>0)
	{
		$screens[] = 5;
	}
	$screens[] = 6;
	$screens = array_unique($screens);
	sort($screens);
	$_SESSION['install']['extract'] = array();
	$_SESSION['install']['screens'] = $screens;
	$next_screen = array_shift($screens);
	$_SESSION['install']['screen'] = $next_screen;
	echo '
updateUpdateStepCurrent("'.$_SESSION['install']['package']['name'].'", "'.$_SESSION['install']['package']['version'].'", '.$next_screen.')
updatePerform("'.$_SESSION['install']['id'].'", "«!ADMIN_INSTALL_'.$install_pages[$next_screen].'_HEAD»", "«!ADMIN_INSTALL_'.$install_pages[$next_screen].'_STATUS»");
';
	if(in_array($next_screen, array(4,5)))
	{
		echo 'updateStartProgress()';
	}
}
?>